<template>
  <el-card shadow="never" header="个人信息">
    <el-form ref="form" :model="form" label-width="120px" style="margin-top:20px;">
      <el-form-item label="头像">
        <el-upload
            class="avatar-uploader"
            ref="upload"
            :limit="1"
            :auto-upload="false"
            :show-file-list="false"
            :on-change="onChange"
        >
          <img v-if="form.avatar" :src="form.avatar?''+form.avatar:'@/assets/avatar.gif'" class="avatar"/>
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>

      <el-form-item label="账号">
        <el-input v-model="form.userName" disabled></el-input>
        <div class="el-form-item-msg">账号信息用于登录，系统不允许修改</div>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="form.nickName"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="form.sex" placeholder="请选择">
          <el-option label="男" value="0"></el-option>
          <el-option label="女" value="1"></el-option>
          <el-option label="保密" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveUserInfo()">保存</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      form: {
        id: 0,
        userName: "",
        nickName: "xiaose",
        sex: "0",
        email: "qingshanking@vip.qq.com",
        phone: null,
        avatar:''
      }
    }
  },
  watch: {},
  mounted() {
    this.loadUserInfo();
  },
  methods: {
    async saveUserInfo() {
      console.log(this.form)
      let id = this.$TOOL.data.get("USER_INFO").userId;
      this.form.id = id;
      let res = await this.$API.system.user.update.put(this.form)
      if (res.code == 200) {
        this.$message.success("保存成功，即将刷新页面重新加载数据")
        //修改缓存中的数据
        let user_info = this.$TOOL.data.get("USER_INFO");
        let user_data = this.$TOOL.data.get("USER_DATA");
        user_info.userName = this.form.nickName;
        Object.assign(user_data, this.form)
        this.$TOOL.data.set("USER_INFO", user_info)
        this.$TOOL.data.set("USER_DATA", user_data)
        setTimeout(() => {
          location.reload()
        }, 1000)
      } else {
        this.$alert(res.msg, "提示", {type: 'error'})
      }
      console.log(res);
    },
    //上传文件转base64
    onChange(file){
      this.getBase64(file.raw).then(res => {
        this.form.avatar = res;
      })
    },
    //这里是文件转base64
    getBase64(file) {
      return new Promise(function (resolve, reject) {
        const reader = new FileReader()
        let imgResult = ''
        reader.readAsDataURL(file)
        reader.onload = function () {
          imgResult = reader.result
        }
        reader.onerror = function (error) {
          reject(error)
        }
        reader.onloadend = function () {
          resolve(imgResult)
        }
      })
    },

    loadUserInfo() {
      let data = this.$TOOL.data.get("USER_DATA");
      Object.assign(this.form, data)
    }
  }
}
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
